package com.intexh.quickandroid.module.chat.ui.messageItemView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.intexh.quickandroid.R;
import com.intexh.quickandroid.module.chat.ChatHelper;
import com.intexh.quickandroid.module.chat.ChatManager.ChatController;
import com.intexh.quickandroid.module.chat.bean.ChatMessage;
import com.intexh.quickandroid.utils.glide.GlideHelper;

import java.util.List;


/**
 * Created by engine on 13-12-17.
 */
public class ImageMessageItem extends AbsChatItem {

    private List<ChatMessage> mList;

    public ImageMessageItem(Context context, ChatMessage message, List<ChatMessage> list, boolean needTime) {
        this(context,message,list,needTime,false);
    }

    public ImageMessageItem(Context context, ChatMessage message, List<ChatMessage> list, boolean needTime, boolean isNearChat) {
        super(context);
        this.mMessage = message;
        this.mNeedTime = needTime;
        this.mContext = context;
        this.mList = list;
       if (ChatHelper.getInstance().getUserInfo().getUid().equals(message.getUid())) {//发出去的
            LayoutInflater.from(context).inflate(R.layout.message_image_to, this);
        } else {//接收到的
            LayoutInflater.from(context).inflate(R.layout.message_image_from, this);
        }
        setLayout();
    }

    private void setLayout() {

        setCommonLayout();

        final ImageView contentTextView = (ImageView) findViewById(R.id.session_content);
        updateImage(contentTextView);
        contentTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	if (mMessage.getContent() != null) { //网络
                    ChatController.INSTANCE.showImage(mContext,mMessage.getContent(),mList);
                }else if (mMessage.getLocalUrl() != null) {
                    ChatController.INSTANCE.showImage(mContext,mMessage.getLocalUrl(),mList);
                }
            }
        });
    }

//    @Override
//    protected void updateSendingStatus() {
//        super.updateSendingStatus();
//        updateImage();

//        ProgressBar progressBar = (ProgressBar) findViewById(R.id.imageUploadProgressBar);
//        if (progressBar == null)
//            return;
//
//        if (mMessage.getProgress() < 0 || mMessage.getProgress() == 1)
//            progressBar.setVisibility(INVISIBLE);
//        else
//            progressBar.setVisibility(VISIBLE);

//    }


    private void updateImage(ImageView contentImage) {
        if (mMessage.getLocalUrl() != null && mMessage.getUid().equals(ChatHelper.getInstance().getUserInfo().getUid())) {
            GlideHelper.INSTANCE.loadImage(contentImage,"file:///"+mMessage.getLocalUrl());
        }else if (mMessage.getContent()!= null) {
            String url = thumbnailUrl(mMessage);
            GlideHelper.INSTANCE.loadImage(contentImage,url);
        }
    }

    public static String thumbnailUrl(ChatMessage chatMessage) {
        if (chatMessage.getType() != ChatMessage.TYPE_IMAGE) {
            return null;
        }
        String url = chatMessage.getContent();
        int width = 500;
        int height = 500;

        if (url != null && url.startsWith("http")) {
            if (!url.contains("?"))
                url += "?";
          //限定长边，短边自适应缩放，将目标图片限制在指定宽高矩形内。详见http://developer.qiniu.com/docs/v6/api/reference/fop/image/imagemogr2.html
            url += "imageMogr2/thumbnail/"+ width +"x" + height;	
           
        }
        return url;
    }
}
